//
//  WBMainTabBarController.swift
//  WeiBo
//
//  Created by 韩菲菲 on 16/8/9.
//  Copyright © 2016年 韩菲菲. All rights reserved.
//

import UIKit

class WBMainTabBarController: UITabBarController {
    
    override func viewDidLoad() {
        super.viewDidLoad()

        //通过KVC设置只读属性
        let btn = WBTabBar()
        btn.tabBarDelegate = self
        
        setValue(btn, forKey: "tabBar")
        
        //添加子控制器
        addChildViewControllers()
 
    }
  
    //添加控制器
    func addChildViewControllers(){
        
        addChildViewController(WBHomeViewController(), imageName: "tabbar_home", title: "首页")
        addChildViewController(WBMessageViewController(), imageName: "tabbar_message_center", title: "消息")
        addChildViewController(WBDiscoverViewController(), imageName: "tabbar_discover", title: "发现")
        addChildViewController(WBProfileViewController(), imageName: "tabbar_profile", title: "我")
    }
    
    //设置子控制器
    func addChildViewController(childController: UIViewController , imageName: String , title: String){
        //设置导航标题
        childController.title = title
        //设置标签标题
        childController.tabBarItem.title = title
        //设置标签颜色
        childController.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName:AppColor], forState: .Selected)
        //设置选中图片， 并且修改选染模式
        childController.tabBarItem.selectedImage = UIImage(named: "\(imageName)_selected")?.imageWithRenderingMode(.AlwaysOriginal)
        //设置普通图片
        childController.tabBarItem.image = UIImage(named: imageName)?.imageWithRenderingMode(.AlwaysOriginal)
        
        //添加子控制器
        addChildViewController(UINavigationController(rootViewController: childController))
        
    }
   
    
}

extension WBMainTabBarController:WBTabBarDelegate{

    
    func tabBarBtnClick() {
 
        //判断登录没登录
        if WBUserAccountViewModel.sharedUserAccountViewModel.isLogin{
        
            //登录了
            //创建发布微博的控制器
            let vc = WBComposeViewController()
            
            //跳转控制器
            self.presentViewController(UINavigationController(rootViewController: vc), animated: true, completion: nil)
            
        }else
        {
            print("用户没登录")
        
        }
        
    }
 
}


















